import 'package:flutter/material.dart';
import 'package:ibooks/const/constant.dart';
import 'package:ibooks/service/system.dart';

class BatteryWidget extends StatefulWidget {
//  final int level;

  final bool isCharging;

  BatteryWidget({this.isCharging = false});

  @override
  _BatteryWidgetState createState() => _BatteryWidgetState();
}

class _BatteryWidgetState extends State<BatteryWidget> {
  int _level = 0;

  final SystemService _service = new SystemService();

  @override
  void initState() {
    super.initState();
    _level = _service.getInt(Constant.batteryLevel);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: <Widget>[
          Container(
            width: 1,
            height: 4,
            color: Colors.black.withOpacity(0.7),
          ),
          Container(
            width: 18,
            height: 10,
            decoration: BoxDecoration(
                border:
                    Border.all(width: 1, color: Colors.black.withOpacity(0.7)),
                borderRadius: BorderRadius.circular(2),
                color: Colors.transparent),
            child: Stack(
              children: _getBatteryInner(),
              alignment: Alignment.centerRight,
            ),
          )
        ],
      ),
    );
  }

  _getBatteryInner() {
    List<Widget> inners = [];
    inners.add(Container(
      height: 10,
      width: 18,
    ));
    inners.add(Container(
      color: Colors.black.withOpacity(0.7),
      width: 15 * (_level / 100),
    ));
    if (widget.isCharging) {
      inners.add(Container(
        child: Icon(
          Icons.flash_on,
          color: Colors.white,
          size: 7,
        ),
        alignment: Alignment.center,
      ));
    }
    return inners;
  }
}
